package oop.ex3.messages;

import java.util.HashMap;

import oop.ex3.messages.exception.InvalidHeaderException;

public class ValidHeaders {
	
	public final static String CONTAINFILE = "CONTAINFILE";
	public final static String DONTCONTAINFILE = "DONTCONTAINFILE";
	public final static String CONTAINNAMESERVER = "CONTAINNAMESERVER";
	public final static String WANTFILE = "WANTFILE";
	public final static String WANTALLFILES = "WANTALLFILES";
	public final static String WANTSERVERS = "WANTSERVERS";
	public final static String GOODBYE = "GOODBYE";
	public final static String GOAWAY = "GOAWAY";
	public final static String WELCOME = "WELCOME";
	public final static String FILEADDRESS = "FILEADDRESS";
	public final static String DONE = "DONE";
	public final static String BEGIN = "BEGIN";
	public final static String ENDLIST = "ENDLIST";
	public final static String ENDSESSION = "ENDSESSION";
	public final static String NSCONTAINFILE = "NSCONTAINFILE";
	public static final String FILE = "FILE";
	public static final String ERROR = "ERROR";
	public static final String FILENOTFOUND = "FILENOTFOUND";
	
	public final static String FILE_CONTENT = "ileContent";
	public final static String FILE_MESSAGE = "file";
	public final static String PEER_INFO = "peerInfo";
	public final static String SIMPLE_MESSAGE = "simpleMessage";
	
	private static HashMap<String, String> _messageType = new HashMap<String, String>();
	private static Boolean _initialized = false;
	
	public static String getMsgType(String header) throws InvalidHeaderException{
		if (!_initialized){
			createMsgTypeMap();
		} 

		if (!_messageType.containsKey(header)){
			throw new InvalidHeaderException();
		} 
		return _messageType.get(header);
	}

	private synchronized static void createMsgTypeMap() {
		if (_initialized){
			return;
		}
		_messageType.put(CONTAINFILE, FILE_MESSAGE);
		_messageType.put(CONTAINNAMESERVER, PEER_INFO);
		_messageType.put(DONE, SIMPLE_MESSAGE);
		_messageType.put(DONTCONTAINFILE, FILE_MESSAGE);
		_messageType.put(ENDLIST, SIMPLE_MESSAGE);
		_messageType.put(ENDSESSION, SIMPLE_MESSAGE);
		_messageType.put(FILE, FILE_CONTENT);
		_messageType.put(BEGIN, PEER_INFO);
		_messageType.put(FILEADDRESS, PEER_INFO);
		_messageType.put(GOODBYE, SIMPLE_MESSAGE);
		_messageType.put(GOAWAY, SIMPLE_MESSAGE);
		_messageType.put(NSCONTAINFILE, FILE_MESSAGE);
		_messageType.put(WANTALLFILES, SIMPLE_MESSAGE);
		_messageType.put(WANTFILE, FILE_MESSAGE);
		_messageType.put(WANTSERVERS, SIMPLE_MESSAGE);
		_messageType.put(WELCOME, SIMPLE_MESSAGE);
		_messageType.put(ERROR, SIMPLE_MESSAGE);
		_initialized = true;
	}
	
}
